package com.link2room.aeriabots.facade.impl;

import com.link2room.aeriabots.dto.param.inside.WxPromotionTransfersInsideParamDto;
import com.link2room.aeriabots.dto.result.base.WxPromotionTransfersResDto;
import com.link2room.aeriabots.entity.*;
import com.link2room.aeriabots.facade.IWxPayFacadeService;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.enums.exception.AeriabotsSysExceptionEnum;
import com.tencent.client.WXPayClient;
import com.tencent.common.Util;
import com.tencent.protocol.pay.qiYq_pay_protocol.PromotionTransfersReqData;
import com.tencent.protocol.pay.qiYq_pay_protocol.PromotionTransfersResData;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Created by Nakisa on 2016/9/22.
 */
@Component("wxPayFacadeService")
public class WxPayFacadeServiceImpl extends BaseFacade implements IWxPayFacadeService {
    @Resource
    private ICommonService commonServiceSupport;


    @Override
    public WxPromotionTransfersResDto requestPromotionTransfers(WxPromotionTransfersInsideParamDto paramDto) throws Exception {
        String partner_trade_no = paramDto.getPartnerTradeNo();
        String openid = paramDto.getOpenid();
        String re_user_name = paramDto.getReUserName();
        BigDecimal amount = paramDto.getAmount();
        String desc = paramDto.getDesc();
        String chechName = paramDto.getCheckName();
        String spbill_create_ip = paramDto.getSpBillCreateIP();
        if(StringUtil.isBlank(partner_trade_no) ||StringUtil.isBlank(openid) ||StringUtil.isBlank(re_user_name) ||
                amount==null||amount.compareTo(BigDecimal.ZERO) <= 0 ||StringUtil.isBlank(desc) ||StringUtil.isBlank(spbill_create_ip) ){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+
                    "\npartner_trade_no:"+partner_trade_no+"\n"+ "\nopenid:"+openid+"\n"+"\nre_user_name:"+re_user_name+"\n"+"\namount:"+amount+"\n"
                    + "\ndesc:"+desc+"\n"
                    +"\nspbill_create_ip:"+spbill_create_ip+"\n", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }

        WXPayClient client = getPromotionTransfersControllerCertWxClient(paramDto.getLhotelGroupCode());
        PromotionTransfersReqData promotionTransfersData;
        promotionTransfersData = new PromotionTransfersReqData(client.getConfigure(),partner_trade_no,openid,re_user_name,amount.multiply(new BigDecimal(100)).intValue(),desc,spbill_create_ip);
        if(StringUtil.isNotBlank(chechName)){
            promotionTransfersData.setCheck_name(chechName);
        }
        String reqStr = XmlUtil.toXML(promotionTransfersData);
        String resStr = "";
        PromotionTransfersResData resData =null;
        try{
            resStr = client.requestPromotionTransfers(promotionTransfersData);
            resData = (PromotionTransfersResData) Util.getObjectFromXML(resStr, PromotionTransfersResData.class);
            if("FAIL".equals(resData.getReturn_code())){
                throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode(), AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+" return_msg:"+resData.getReturn_msg()+" Err_code: "+resData.getErr_code()+"Err_code_des : "+resData.getErr_code_des(), AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
            }
            if("FAIL".equals(resData.getResult_code())){
                throw new SysException(AeriabotsSysExceptionEnum.WX_SDK_ERROR.getCode(), AeriabotsSysExceptionEnum.WX_SDK_ERROR.getMsg()+" return_msg:"+resData.getReturn_msg()+" Err_code: "+resData.getErr_code()+"Err_code_des : "+resData.getErr_code_des(), AeriabotsSysExceptionEnum.WX_SDK_ERROR.getBizType());
            }
        }finally {
            //log
            WxPromotionTransfersLog log = new WxPromotionTransfersLog();
            log.setLhotelGroupCode(paramDto.getLhotelGroupCode());
            log.setOpenid(openid);
            log.setPartnerTradeNo(partner_trade_no);
            log.setAppid(client.getConfigure().getAppid());
            log.setReqXml(reqStr);
            log.setResXml(resStr);
            if(resData !=null){
                log.setPaymentNo(resData.getPayment_no());
                log.setResultCode(resData.getResult_code());
            }
            commonServiceSupport.save(log);
        }
        WxPromotionTransfersResDto resDto = new WxPromotionTransfersResDto();
        resDto.setPartnerTradeNo(partner_trade_no);
        resDto.setPaymentNo(resData.getPayment_no());
        return  resDto;
    }

}
